package com.sinocare.dict.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sinocare.common.entity.*;
import com.sinocare.common.result.Result;
import com.sinocare.common.result.ResultCode;
import com.sinocare.dict.service.PatientService;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/patient")
@ApiModel(value = "检测者记录管理",description = "检测者列表")
@CrossOrigin        //跨域请求
public class PatientController {

    @Resource
    private PatientService patientServiceImpl;

    /**
     * 查询总店信息
     * @param page
     * @param limit
     * @param searchParams
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "查询检测者列表信息",notes = "模糊查询、分页",httpMethod = "GET")
    @RequestMapping(value = "queryPatient",method = RequestMethod.GET)
    public Result findPatients(Integer page,Integer limit,String searchParams) throws Exception{

        ObjectMapper objectMapper = new ObjectMapper();
        Patient patient = null;
        if (searchParams!=null){
            patient = objectMapper.readValue(searchParams, Patient.class);
        }
        Page page1 = PageHelper.startPage(page, limit);
        List<Patient> patients = patientServiceImpl.queryPatient(patient);
        return Result.ok(ResultCode.SUCCESS,page1.getTotal(),patients);
    }

    /**
     * 所属分店下拉框
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "queryStoreName",method = RequestMethod.GET)
    public Result findStoreName() throws Exception{
        List<Store> storeName = patientServiceImpl.queryStoreName();
        return Result.ok(ResultCode.SUCCESS,storeName);
    }

    /**
     * 新增检测者
     * @param patient
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "addPatient", method = RequestMethod.POST)
    @ApiOperation(value = "新增检测者", notes = "新增检测者", httpMethod = "POST")
    public Result addPatient(Patient patient) throws Exception{
        System.out.println(patient);
        patient.setUserId(3);
        int i = patientServiceImpl.addPatient(patient);
        if(i>0){
            return Result.ok(ResultCode.SUCCESS, i);
        }else{
            return Result.fail(ResultCode.FAIL, null);
        }
    }

    /**
     * 修改检测者信息
     * @param patient
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "alterPatient", method = RequestMethod.POST)
    @ApiOperation(value = "修改检测者信息",httpMethod = "POST")
    public Result alterPatient(Patient patient) throws Exception{
        System.out.println(patient);
        int i = patientServiceImpl.alterPatient(patient);
        if(i>0){
            return Result.ok(ResultCode.SUCCESS, i);
        }else{
            return Result.fail(ResultCode.FAIL, null);
        }
    }

    /**
     * 删除检测者信息
     * @param patientId
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "deletePatient/{patientId}", method = RequestMethod.POST)
    @ApiOperation(value = "删除检测者信息",httpMethod = "POST")
    public Result deletePatient(@PathVariable("patientId") Integer patientId) throws Exception{
        System.out.println(patientId);
        int i = patientServiceImpl.deletePatient(patientId);
        if(i>0){
            return Result.ok(ResultCode.SUCCESS, i);
        }else{
            return Result.fail(ResultCode.FAIL, null);
        }
    }

    /**
     * 新增检测
     * @param patient
     * @param medicalResult
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "addMedicalResult", method = RequestMethod.POST)
    @ApiOperation(value = "新增检测", notes = "新增检测", httpMethod = "POST")
    public Result addMedicalResult(Patient patient, MedicalResult medicalResult) throws Exception{

        System.out.println(patient);

        List<Patient> patients = patientServiceImpl.queryCountTest();
        for (Patient patient1 : patients) {
            if (patient1.getPatientId()==patient.getPatientId()) {
                if (patient1.getCountOfTest()==null) {
                    patient.setCountOfTest(1);
                } else {
                    patient.setCountOfTest(patient1.getCountOfTest()+1);
                }
            }
        }



        Double weight = patient.getWeight();
        Double height = patient.getHeight();
        if (height!=null&&weight!=null) {
            Double hei = height/100;
            Double bmi = weight / (hei * hei);
            medicalResult.setBmiResult(bmi);
        }
        int i1 = patientServiceImpl.alterBMI(patient);
        int i2 = patientServiceImpl.addMedicalResult(medicalResult);
        if(i1>0&&i2>0){
            return Result.ok(ResultCode.SUCCESS,1);
        }else{
            return Result.fail(ResultCode.FAIL, null);
        }
    }

    /**
     * 查询检测报告信息
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "查询检测报告信息",httpMethod = "GET")
    @RequestMapping(value = "questReportList",method = RequestMethod.GET)
    public Result findReportList(ReportList reportList) throws Exception{

        List<ReportList> reportLists = patientServiceImpl.questReportList(reportList);

        return Result.ok(ResultCode.SUCCESS,reportLists);
    }


}
